package com.lsk.servicemap.remote;


import com.lsk.internalcommon.DTO.ResponseResult;
import com.lsk.internalcommon.constant.AMapConfigConstants;
import com.lsk.internalcommon.response.DirectionResponse;
import com.lsk.internalcommon.response.GaodeResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class GaodeClient {
    @Value("${map.key}")
    private String mapKey;

    @Autowired
    private RestTemplate restTemplate;

    public ResponseResult addService(String name){
        String url = spell(name);
        //调用请求
        ResponseEntity<String> direction = restTemplate.postForEntity(url,null,String.class);
        String body = direction.getBody();
        //解析返回参数
        JSONObject result = JSONObject.fromObject(body);
        JSONObject data = result.getJSONObject("data");
        String sid = data.getString("sid");
        GaodeResponse response = new GaodeResponse();
        response.setSid(sid);
        return ResponseResult.success(response);
    }

    //拼接url
    private String spell(String name){
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(AMapConfigConstants.SERVICE_ADD_URL);
        urlBuilder.append("?");
        urlBuilder.append("key="+mapKey);
        urlBuilder.append("&");
        urlBuilder.append("name="+name);
        return urlBuilder.toString();
    }

}
